
import 'package:cunyutech_flutter_ui/src/button/main.dart';
import 'package:cunyutech_flutter_ui/src/funcs/main.dart';
import 'package:cunyutech_flutter_ui/src/text/main.dart';
import 'package:cunyutech_flutter_ui/src/theme/main.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';


class ConfirmExtSubtitle extends StatelessWidget {
  const ConfirmExtSubtitle(this.title, {Key key }) : super(key: key);

  final String title;

  @override
  Widget build(BuildContext context) {
    var cs = CYThemeData.of(context);
    return CYParagraph(
      title, 
      fontSize: CYFontSize.Default, 
      color: cs.backColorSystem.secondaryColor, 
      lineHeight: null,
      margin: EdgeInsets.symmetric(horizontal: CYMargin.Default),
      textAlign: TextAlign.center,
      alignment: Alignment.center,
    );
  }
}

class ConfirmExtAction {
  ConfirmExtAction(this.title, { this.backgroundColor, this.type = PrimaryButtonType.PRIMARY });

  ConfirmExtAction.cancel(this.title, {
    this.backgroundColor, this.type = PrimaryButtonType.DEFAULT
  });

  final String title;
  final Color backgroundColor;
  final PrimaryButtonType type;

  create() {
    return PrimaryButtonProps(type: type, title: title, backgroundColor: backgroundColor, margin: EdgeInsets.only(top: CYMargin.Default, right: CYMargin.Default, left: CYMargin.Default));
  }
}

Future<int> showConfirmDialog({
    BuildContext rootContext, 
    String title = "提示",
    List<PrimaryButtonProps> actions,
    bool barrierDismissible = false,
    Widget child,
  }) {
    var cs = CYThemeData.of(rootContext);
    Color backgroundColor = cs.brightness  == Brightness.dark ? cs.primaryForeColor : cs.primaryBackColor;
    Color color = cs.brightness == Brightness.dark ? cs.primaryBackColor : cs.primaryForeColor;

    return showDialogExt<int>(
      builder: (BuildContext context) {
        Widget content = Container(
          height: CYScreen.height,
          alignment: Alignment.center,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            mainAxisSize: MainAxisSize.min,
            children: [
              Container(
                margin: EdgeInsets.symmetric(horizontal: CYMargin.Default),
                decoration: BoxDecoration(
                  color: backgroundColor,
                  borderRadius: BorderRadius.circular(14),
                ),
                child: Column(
                  children: <Widget>[
                    CYParagraph(
                      title, 
                      color: color, 
                      lineHeight: null, 
                      fontWeight: FontWeight.w600, 
                      fontSize: CYFontSize.Large, 
                      margin: child == null ? EdgeInsets.symmetric(vertical: CYMargin.Large, horizontal: CYMargin.Default) : EdgeInsets.only(top:  CYMargin.Large, right:  CYMargin.Default, left: CYMargin.Default),
                      alignment: Alignment.center,
                      textAlign: TextAlign.center,
                    ),
                    child,
                    Container(
                      margin: EdgeInsets.only(bottom: 32),
                      alignment: Alignment.center,
                      child: Column(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: actions.map((item){
                          item.onTap = () {
                            Navigator.of(context).pop(actions.indexOf(item));
                          };
                          return PrimaryButton.withProps(item);
                        }).toList()
                      )
                    ),
                  ].where((item)=> item != null).toList(),
                ),
              ),
            ]
          ),
        );

        if(barrierDismissible == false) {
          content = ClickTwiceBackToHome(
            child: Scaffold(
              backgroundColor: Colors.transparent,
              body: content,
            )
          );
        }
        return content;
      }, 
      context: rootContext
    );
  }


Future<T> showDialogExt<T>({
  @required BuildContext context,
  bool barrierDismissible = true,
  WidgetBuilder builder,
  Color backgroundColor = Colors.black26,
}) {
  assert(debugCheckHasMaterialLocalizations(context));
  var _builder = cyThemeWrapBuilder(builder, context);
  
  return showGeneralDialog(
    context: context,
    pageBuilder: (BuildContext buildContext, Animation<double> animation, Animation<double> secondaryAnimation) {
      return Builder(
        builder: _builder,
      );
    },
    barrierDismissible: barrierDismissible,
    barrierLabel: MaterialLocalizations.of(context).modalBarrierDismissLabel,
    barrierColor: backgroundColor,
    transitionDuration: const Duration(milliseconds: 150),
    transitionBuilder: _buildMaterialDialogTransitions,
  );
}

Widget _buildMaterialDialogTransitions(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
  return FadeTransition(
    opacity: CurvedAnimation(
      parent: animation,
      curve: Curves.easeOut,
    ),
    child: child,
  );
}
